//
//  WelcomeViewController.m
//  weibo
//
//  Created by hp on 16/5/4.
//  Copyright © 2016年 hp. All rights reserved.
//

#import "WelcomeViewController.h"
// 省略mas_开头
#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS
#import <Masonry.h>
#import "UserAccount.h"
#import <UIImageView+WebCache.h>
@interface WelcomeViewController ()
@property (nonatomic,strong) UIImageView *bgImageView;
@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *welcomeLabel;



@end

@implementation WelcomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setupUI];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [UIView animateWithDuration:2 delay:1 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseIn animations:^{
       // 修改约束
        [self.iconImageView updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.top).offset(-100);
        }];
        // 重新布局
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        
        // 进入首页
        NSLog(@"进入首页");
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationSwitchRootViewController object:nil userInfo:@{@"VC":@"Main"}];
    }];
}

- (void)setupUI
{
    // 添加子控件
    [self.view addSubview:self.bgImageView];
    [self.view addSubview:self.iconImageView];
    [self.view addSubview:self.welcomeLabel];
    
    // 约束
    [self.bgImageView makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.view);
    }];
    [self.iconImageView makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.centerX);
        make.top.equalTo(self.view.top).offset(150);
        make.width.height.equalTo(100);
    }];
    [self.welcomeLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.centerX);
        make.top.equalTo(self.iconImageView.bottom).offset(30);
    }];
    
    // 设置用户头像
    UserAccount *userAccount = [UserAccount sharedUserAccount];
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:userAccount.avatar_large]];
}

#pragma  mark - 懒加载子控件
- (UIImageView *)bgImageView
{
    if (_bgImageView==nil) {
        _bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ad_background"]];
    }
    return _bgImageView;
}

- (UIImageView *)iconImageView
{
    if (_iconImageView==nil) {
        _iconImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar_default_big"]];
        _iconImageView.layer.cornerRadius = 50;
        _iconImageView.layer.masksToBounds = YES;
    }
    
    return _iconImageView;
}

- (UILabel *)welcomeLabel
{
    if (_welcomeLabel==nil) {
        _welcomeLabel = [[UILabel alloc] init];
        _welcomeLabel.text = @"欢迎回来";
        [_welcomeLabel sizeToFit];
    }
    return _welcomeLabel;
}
@end

